var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var morgan = require('morgan');
var session = require('express-session');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var Waterline = require('waterline');
var util = require('./utils/util');
var waterlineTool = require('./utils/waterlineTool.js');
var fs = require('fs');
var multiparty = require('multiparty');
var http = require('http');


var app = express();
var logger = require('./utils/logger');

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

var sessionOption = {
    secret: 'cat key',
    resave: false,
    saveUninitialized: true,
    cookie: {path: '/smtool_project', maxAge: 2 * 60 * 60 * 1000}
};

app.use(session(sessionOption));
app.locals.dateFormat = util.dateFormat;

//不是开发环境，将console打印信息写入日志文件 app.get('env')!=='development'
logger.replaceConsole(true);

//初始化mysql数据库
util.waterline = new Waterline();
waterlineTool.loadPo();
waterlineTool.initDatabase();

app.use(function(req, res, next){
    res.locals.userinfo = req.session.userinfo || {};
    res.locals.channelportal = req.session.channelportal;
    next();
});

//管理平台页面
app.use('/smtool_project/surveying', require('./routes/view/surveying'));
//登录页面
app.use('/smtool_project/login', require('./routes/view/login'));



//获取数据库数据
//登录
app.use('/smtool_project/api/login', require('./routes/api/login'));

//运行状态
app.use('/smtool_project/api/evaluate', require('./routes/api/evaluate'));
app.use('/smtool_project/api/seller', require('./routes/api/seller'));
app.use('/smtool_project/api/knowledgeBase', require('./routes/api/knowledge-base'));
app.use('/smtool_project/api/schedule', require('./routes/api/schedule'));
app.use('/smtool_project/api/surveying', require('./routes/api/surveying'));
app.use('/smtool_project/api/roadshow', require('./routes/api/roadshow'));
app.use('/smtool_project/api/teleconference', require('./routes/api/teleconference'));
app.use('/smtool_project/api/strategyMeeting', require('./routes/api/strategy-meeting'));
app.use('/smtool_project/api/recommond', require('./routes/api/recommond'));
app.use('/smtool_project/api/stockRecommend', require('./routes/api/stockrecommend'));
app.use('/smtool_project/api/morningconference', require('./routes/api/morningconference'));

app.use('/smtool_project/api/otherService', require('./routes/api/other-service'));
app.use('/smtool_project/api/contact', require('./routes/api/contact'));
app.use('/smtool_project/api/user', require('./routes/api/user'));
app.use('/smtool_project/api/userjoin', require('./routes/api/userjoin'));

app.use('/smtool_project/api/team', require('./routes/api/team'));
app.use('/smtool_project/api/teamUser', require('./routes/api/teamUser'));


app.use('/smtool_project/api/dicSurveyingType', require('./routes/api/dic-surveying-type'));
app.use('/smtool_project/api/dicCompany', require('./routes/api/dic-company'));
app.use('/smtool_project/api/dicStock', require('./routes/api/dic-stock'));

app.use('/smtool_project/api/dicSurveyingRange', require('./routes/api/dic-surveying-range'));
app.use('/smtool_project/api/dicRoadshowType', require('./routes/api/dic-roadshow-type'));
app.use('/smtool_project/api/dicRoadshowRange', require('./routes/api/dic-roadshow-range'));
app.use('/smtool_project/api/dicRoadshowLocation', require('./routes/api/dic-roadshow-location'));
app.use('/smtool_project/api/dicStrategyMeetingRange', require('./routes/api/dic-strategy-meeting-range'));
app.use('/smtool_project/api/dicIndustry', require('./routes/api/industry'));


//app
app.use('/smtool_project/wxapp/', require('./routes/view/wxapp'));

app.post("/upload",function(req,res) {
    //设置编辑
    var form = new multiparty.Form();
    //设置文件存储路径
    form.uploadDir = path.normalize("public/images/icon/");
    console.log(form.uploadDir);

    form.parse(req, function(err, fields, files) {
        console.log("=======err======\n");
        console.log(err);
        console.log("=======files======\n");
        console.log(files);
        res.end(path.parse(files.file[0].path).base);
    });
});
// catch 404 and forward to error handler
app.use(function (req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function (err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
        });
    });
}

// production error handler
// no stacktraces leaked to user
app.use(function (err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});




module.exports = app;
